/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecipeListCell.java
 *
 * Created on Jan 27, 2012, 8:50:40 AM
 */
package laboration2.gui;

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import se.chalmers.ait.dat215.lab2.Recipe;

/**
 *
 * @author arildm
 */
public class RecipeListCell extends JPanel implements ListCellRenderer {

    /** Creates new form RecipeListCell */
    public RecipeListCell() {
    initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imageLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        infoLabel = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        difficultyLabel = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getResourceMap(RecipeListCell.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(32767, 40));
        setName("Form"); // NOI18N

        imageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        imageLabel.setText(resourceMap.getString("imageLabel.text")); // NOI18N
        imageLabel.setName("imageLabel"); // NOI18N
        imageLabel.setPreferredSize(new java.awt.Dimension(60, 40));

        nameLabel.setFont(nameLabel.getFont().deriveFont(nameLabel.getFont().getStyle() | java.awt.Font.BOLD));
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        infoLabel.setText(resourceMap.getString("infoLabel.text")); // NOI18N
        infoLabel.setName("infoLabel"); // NOI18N

        timeLabel.setIcon(resourceMap.getIcon("timeLabel.icon")); // NOI18N
        timeLabel.setText(resourceMap.getString("timeLabel.text")); // NOI18N
        timeLabel.setName("timeLabel"); // NOI18N

        priceLabel.setIcon(resourceMap.getIcon("priceLabel.icon")); // NOI18N
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        difficultyLabel.setText(resourceMap.getString("difficultyLabel.text")); // NOI18N
        difficultyLabel.setName("difficultyLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
                        .addComponent(difficultyLabel)
                        .addGap(18, 18, 18)
                        .addComponent(timeLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(infoLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 159, Short.MAX_VALUE)
                        .addComponent(priceLabel))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(timeLabel)
                    .addComponent(difficultyLabel))
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(infoLabel)
                    .addComponent(priceLabel)))
            .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel difficultyLabel;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration//GEN-END:variables

    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        if (value == null || !(value instanceof Recipe)) {
            throw new IllegalArgumentException();
        }
        Recipe recipe = (Recipe) value;
        
        // Do icon
        ImageIcon icon = recipe.getImage();
        if (icon != null && icon.getIconWidth() > 0 && icon.getIconHeight() > 0) {
            int width = imageLabel.getWidth();
            int height = imageLabel.getHeight();
            
            // Method 1: use the scale functionality in Recipe
            imageLabel.setIcon(recipe.getImage(width, height));
            
            // Method 2: uses custom class RecipeIcon to scale nicely.
            /*RecipeIcon scaledIcon = new RecipeIcon(icon.getImage());
            scaledIcon.scaleOptimal(width, height);
            imageLabel.setIcon(scaledIcon);*/
        }
        // Fill rest of labels
        imageLabel.setText(null);
        nameLabel.setText(recipe.getName());
        infoLabel.setText(recipe.getCuisine()+", "+recipe.getMainIngredient());
        difficultyLabel.setText(recipe.getDifficulty());
        timeLabel.setText("ca "+recipe.getTime()+" min");
        priceLabel.setText("ca "+recipe.getPrice()+" kr");
        
        // Fetch default style using default renderer and a dummy list
        ListCellRenderer defaultRenderer = new DefaultListCellRenderer();
        JLabel defaultLabel = (JLabel) defaultRenderer.
            getListCellRendererComponent(new JList(), null, 0, isSelected,
            cellHasFocus);
	setBorder(defaultLabel.getBorder());
        setForeground(defaultLabel.getForeground());
        if (isSelected || index % 2 == 1) {
            setBackground(defaultLabel.getBackground());
        } else {
            setBackground(java.awt.Color.WHITE);
        }
        
        return this;
    }
}
